package com.obtk.property.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.obtk.property.dao.HouseMapper;
import com.obtk.property.pojo.House;
import com.obtk.property.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;
import java.util.Map;

@Service
public class HouseServiceImpl implements HouseService {
    @Autowired
    private HouseMapper houseMapper;

    @Override
    public List<House> findAll() {
        return houseMapper.selectAll();
    }

    @Override
    public Page<House> search(Map searchMap) {
        //通用Mapper多条件搜索，标准写法
        Example example = new Example(House.class); //指定查询的表
        //1.初始化分页条件
        int pageNum = 1;
        int pageSize = 2;
        if(searchMap != null){
            Example.Criteria criteria = example.createCriteria();
            //时间区间
            if(StringUtil.isNotEmpty((String) searchMap.get("startTime"))){
                criteria.andGreaterThanOrEqualTo("createTime",searchMap.get("startTime"));
            }
            if (StringUtil.isNotEmpty((String) searchMap.get("endTime"))){
                criteria.andGreaterThanOrEqualTo("createTime",searchMap.get("endTime"));
            }
            //名称模糊搜索
            if(StringUtil.isNotEmpty((String) searchMap.get("houseName"))){
                criteria.andLike("houseName","%"+(String) searchMap.get("houseName")+"%");
            }
            //分页

            if((Integer) searchMap.get("pageNum") != null){
                pageNum = (Integer)searchMap.get("pageNum");
            }
            if ((Integer) searchMap.get("pageSize")!=null){
                pageSize = (Integer)searchMap.get("pageSize");
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<House> House = (Page<House>) houseMapper.selectByExample(example);
        return House;
    }

    @Override
    public Boolean add(House house) {
        int row = houseMapper.insert(house);
        if(row>0){
            return true;
        }
        return false;
    }

    @Override
    public House findById(Integer houeId) {
        return houseMapper.selectByPrimaryKey(houeId);
    }

    @Override
    public Boolean update(House house) {
        int row = houseMapper.updateByPrimaryKeySelective(house);
        if(row>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean del(List<Integer> ids) {
        for(Integer id:ids){
            houseMapper.deleteByPrimaryKey(id);
        }
        return true;
    }
}
